package cn.xo68.ide.plugin.common.component;

import cn.xo68.core.date.DateTime;
import cn.xo68.core.date.TimeSpan;
import cn.xo68.ide.plugin.common.ui.toolwindow.ToolSuiteToolWindow;
import com.intellij.execution.filters.TextConsoleBuilderFactory;
import com.intellij.execution.ui.ConsoleView;
import com.intellij.execution.ui.ConsoleViewContentType;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.AbstractProjectComponent;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.CheckForNull;
import javax.swing.*;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Date;

/**
 *  tool window 控制台 项目组件
 * @author wuxie
 */
public class ToolSuiteConsole extends AbstractProjectComponent {

    private final Logger logger = LoggerFactory.getLogger(getClass());
    //public static String LOG_FORMAT="%-d{HH:mm:ss} [%p] %c -  %m%n";


    private final ConsoleView consoleView;

    private Date lastShowLogTagDate;

    public ToolSuiteConsole(Project project) {
        super(project);
        consoleView = TextConsoleBuilderFactory.getInstance().createBuilder(project).getConsole();
        lastShowLogTagDate= DateTime.Now().addSecond(30).getDate();


    }

    @Override
    public void projectOpened() {
        StarupApplicationComponent.getInstance().setLastOperateProject(this.myProject);
        logger.info("项目加载完成,项目：{}", this.myProject.getName());
    }

    public void debug(String msg) {
        getConsoleView().print(msg , ConsoleViewContentType.NORMAL_OUTPUT);
    }

    public void info(String msg) {
        getConsoleView().print(msg , ConsoleViewContentType.NORMAL_OUTPUT);

        showLogToolWindowTag();
    }

    public void error(String msg) {
        getConsoleView().print(msg , ConsoleViewContentType.ERROR_OUTPUT);

        showLogToolWindowTag();

    }

    public void error(String msg, Throwable t) {
        error(msg);
        StringWriter errors = new StringWriter();
        t.printStackTrace(new PrintWriter(errors));
        error(errors.toString());
    }

    public void clear() {
        getConsoleView().clear();
    }

    public ConsoleView getConsoleView() {
        return this.consoleView;
    }

    private ToolWindow getToolWindow() {
        ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(myProject);
        return toolWindowManager.getToolWindow(ToolSuiteToolWindow.TOOL_WINDOW_ID);
    }

    private void showLogToolWindowTag(){
        TimeSpan timeSpan = TimeSpan.Substract(DateTime.Now(), new DateTime(lastShowLogTagDate));
        if(timeSpan.getTotalseconds() <= 10){
            return;
        }
        lastShowLogTagDate = DateTime.Now().getDate();

        ApplicationManager.getApplication().invokeLater(()->{
            ToolWindow toolWindow = getToolWindow();
            if (toolWindow != null) {
                toolWindow.show(() -> selectTab(toolWindow, ToolSuiteToolWindow.TAB_LOGS));
            }
        });

    }

    @CheckForNull
    private static JComponent selectTab(ToolWindow toolWindow, String tabId) {
        ContentManager contentManager = toolWindow.getContentManager();
        Content content = contentManager.findContent(tabId);
        if (content != null) {
            contentManager.setSelectedContent(content);
            return content.getComponent();
        }
        return null;
    }
}
